package com.itcam.interview.pro00.thread;

/**
 * @author : Cammy.Wu
 * Description : 
 */

public class InterruptAndInterruptedExample {
    public static void main(String[] args) {
//        extracted();
        Thread.currentThread().interrupt();
        boolean isInterrupted = Thread.interrupted();
        System.out.println("第一次检查中断状态: " + isInterrupted);
        isInterrupted = Thread.interrupted();
        System.out.println("第二次检查中断状态: " + isInterrupted);
    }

    private static void extracted() {
        Thread thread = new Thread(() -> {
            while (true) {
                if (Thread.currentThread().isInterrupted()) {
                    System.out.println("线程被中断");
                    break;
                }
                System.out.println("线程正在执行任务...");
            }
        });
        thread.start();
        // 主线程休眠1秒，确保子线程有足够时间执行
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        thread.interrupt();
    }
}
